﻿/*****************************************************************************/
/* Stored Procedure :: SaveApplicant                                         */
/*****************************************************************************/
/* Copyright (C) 2008, Perot Systems Corporation. All right reserved.        */
/* Developer: Ed. Mueller (edward.mueller@ps.net)                            */
/* Started:  02/12/2008                                                      */
/*****************************************************************************/
CREATE PROCEDURE [Kernel].[SaveApplicant]
@OID			                bigint			= NULL,
@Version		                rowversion		= NULL,
@KinshipID                      bigint          = NULL,
@AddressID                      bigint          = NULL,
@FirstName		                varchar(128)	= NULL,
@LastName		                varchar(128)	= NULL,
@MiddleInitial	                char(01)		= NULL,
@DOB			                smalldatetime	= NULL,
@Sex			                char(01)		= NULL,
@Phone1			                varchar(20)		= NULL,
@Phone2			                varchar(20)		= NULL,
@eMailAddress	                varchar(128)	= NULL
AS 

---------------------------------------------------------------------------------------------------
--						Liberty
---------------------------------------------------------------------------------------------------
--  Stored Procedure :: SaveApplicant
--                                                                            
---------------------------------------------------------------------------------------------------

SET NOCOUNT ON 

DECLARE @RetCode int,
        @RowCount int,
        @PersonVersion rowversion

SELECT @PersonVersion = NULL

-- 1. Save the fields associated with the person.
EXEC @RetCode = Kernel.SavePerson @OID OUTPUT, @PersonVersion OUTPUT, @FirstName, @LastName, @MiddleInitial, @DOB, @Sex, @Phone1, @Phone2, @eMailAddress
IF (@RetCode <> 0) Goto ExitError

-- 2. If new record (OID is not set)
IF (NOT EXISTS (SELECT * FROM dbo.Applicants WHERE (OID = @OID)))
BEGIN
	-- 2.1. Perform insert.
	INSERT INTO dbo.Applicants
	(OID, KinshipID, AddressID, UpdatedDate, CreatedDate)
	VALUES
	(@OID, @KinshipID, @AddressID, GETDATE(), GETDATE())
END

-- 3. If OID is set (existing record)
ELSE
BEGIN
	-- 3.1. Attempt updating object (using both OID and timestamp for concurrency reasons)
	UPDATE dbo.Applicants
	SET KinshipID = @KinshipID
	    ,AddressID = @AddressID
		,UpdatedDate = GETDATE()
	WHERE ((OID = @oid) AND (Version = @Version))
    SELECT @RetCode = @@ERROR, @RowCount = @@ROWCOUNT
    IF (@RetCode <> 0) GOTO ExitError

	-- 3.2. If no records were found (and updated) - it means that
	-- another process had updated this record (therefore timestamp will be different)
	-- or record was deleted (OID not found)
	-- Either way, we need to raise exception
	IF @RowCount = 0
	BEGIN
		RAISERROR(99999, 16, 1, 'Applicant' ) --WITH LOG
		GOTO ExitError
	END
END

---------------------------------------------------------------------------------------------------
--	Return the values to the caller
---------------------------------------------------------------------------------------------------
SELECT OID, Version
FROM dbo.Applicants 
WHERE OID = @OID

RETURN(0)

ExitError:
	RETURN(1)


